package com.hcb.tonguetiphealth.widget.homepage;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.JSONManager;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.util.StringUtils;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class HealthCateWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "HealthCateWidget";
	private String url = Global.URL + Global.servlet_healthRecommendCate;
	
	private Activity ctx;
	private Handler mHandler;
	private LinearLayout bt_back;
	private Button bt_ok;
	private EditText et_content, et_reason, et_suitable, et_unsuited;
	private String content, reason, suitable, unsuited;
	private String strMsg;
	
	public HealthCateWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.homepage_personal_cate, this); 
		
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
				case MessageWhat.COMMIT_ING:
					showMessageDialog(ctx, "正在提交...");
					break;
				case MessageWhat.COMMIT_SUCCEED:
					showMessage(ctx, "提交成功");
					dismissMessageDialog();
					break;
				case MessageWhat.COMMIT_FAILD:
					showMessage(ctx, strMsg);
					bt_ok.setClickable(true);
					dismissMessageDialog();
					break;
				}
			}
		};

		et_content = (EditText)findViewById(R.id.health_personal_cate_content_edittext);
		et_reason = (EditText)findViewById(R.id.health_personal_cate_reason_edittext);
		et_suitable = (EditText)findViewById(R.id.health_personal_cate_suitable_edittext);
		et_unsuited = (EditText)findViewById(R.id.health_personal_cate_unsuited_edittext);
		
		bt_back = (LinearLayout)findViewById(R.id.health_personal_cate_back_button);
		bt_ok = (Button)findViewById(R.id.health_personal_cate_commit_button);
		
		bt_back.setOnClickListener(this);
		bt_ok.setOnClickListener(this);
	}
	
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.health_personal_cate_back_button:
			ctx.finish();
			break;
		case R.id.health_personal_cate_commit_button:
			commit();
			break;
		default:
			break;
		}
	}
	private void commit() {
		content = et_content.getText().toString();
		reason = et_reason.getText().toString();
		suitable = et_suitable.getText().toString();
		unsuited = et_unsuited.getText().toString();
		if(StringUtils.isEmpty(content)) {
			showMessage(ctx, "请填写营养内容");
			return;
		}
		if(StringUtils.isEmpty(reason)) {
			showMessage(ctx, "请填写推荐理由");
			return;
		}
		if(StringUtils.isEmpty(suitable)) {
			showMessage(ctx, "请填写适宜群体");
			return;
		}
		if(StringUtils.isEmpty(unsuited)) {
			showMessage(ctx, "请填写忌食群体");
			return;
		}
		
		bt_ok.setClickable(false);
		mHandler.sendEmptyMessage(MessageWhat.COMMIT_ING);
		new Thread() {
			public void run() {
				String response = null;
				try {
					List <NameValuePair> params = new ArrayList <NameValuePair>();
					params.add(new BasicNameValuePair("userId", Global.user.getUserId()));
					params.add(new BasicNameValuePair("content", content));
					params.add(new BasicNameValuePair("reason", reason));
					params.add(new BasicNameValuePair("suitable", suitable));
					params.add(new BasicNameValuePair("unsuited", unsuited));
					response = HttpUtils.Post(url, params);
					
					if(Global.DEBUG) {
						System.out.println("-----------------------------\n" + response);
					}
				} catch(Exception e) {
					Log.e(TAG, "Exception", e);
				}
				if(!StringUtils.isEmpty(response)) {
					JSONManager jManager = new JSONManager();
					boolean isGotJSONObject = jManager.isGotJSONObject(response);
					if(isGotJSONObject) {
						int count = jManager.getResult();
						String msg = jManager.getMsg();
						System.out.println("------ 得到msg: " + count + ":" + msg);
						mHandler.sendEmptyMessage(MessageWhat.COMMIT_SUCCEED);
					} else {
						strMsg = jManager.getMsg();
						mHandler.sendEmptyMessage(MessageWhat.COMMIT_FAILD);
					}
				} else {
					strMsg = "提交失败";
					mHandler.sendEmptyMessage(MessageWhat.COMMIT_FAILD);
				}
			}
		}.start();
	}

	@Override
	public void notifyAdapter() {
		
	}
}
